﻿
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;


namespace Company.Shell.Controls
{
    /// <summary>
    /// Heater.xaml 的交互逻辑
    /// </summary>
    public partial class Heater : UserControl
    {
        private Brush closeBrush;
        private Brush openBrush=new SolidColorBrush(Color.FromRgb(75, 169, 69));
        public Heater()
        {
            InitializeComponent();
            closeBrush= _border.Background;
        }
        public bool IsOpen
        {
            get { return (bool)GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); }
        }
        //定义了一个名为 IsOpen 的依赖属性，用于控制阀门控件的开启/关闭状态
        public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register
            ("IsOpen", typeof(bool), typeof(Heater), new PropertyMetadata(false, new PropertyChangedCallback(isOpenChangedCallback)));

        private static void isOpenChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Heater)d;
            if (control == null)
            {
                return;
            }
            if (e.NewValue is bool isopen)
            {
                if (isopen)
                {
                    control._border.Background = control.openBrush;
                    control._switch.Text = "ON";
                    //开始颜色动画
                    control._path.Stroke.BeginAnimation(SolidColorBrush.ColorProperty,control.colorAnimation);
                }
                else
                {
                    control._border.Background = control.closeBrush;
                    control._switch.Text = "OFF";
                    control._path.Stroke.BeginAnimation(SolidColorBrush.ColorProperty, null);

                }
            }
        }
        /// <summary>
        /// 温度值
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Heater), new PropertyMetadata("00.0°C", new PropertyChangedCallback(TextChangeCallback)));

        private static void TextChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Heater)d;
            if (control == null)
            {
                return;
            }
            if (e.NewValue is string text)
            {
                control._value.Text = text + "°C";
            }
        }

        //创建颜色动画
        private ColorAnimation colorAnimation = new()
        {
            From = Color.FromRgb(219, 219, 219),//动画开始颜色为灰色
            To = Color.FromRgb(255, 0, 0),//动画结束颜色为红色
            Duration = new Duration(TimeSpan.FromSeconds(2)),//动画持续时间为2秒
            AutoReverse = true,//动画完成后自动反转
            RepeatBehavior = RepeatBehavior.Forever//动画无限循环
        };

    }
}
